function LineFaultPart() {
	var lineFaultDatas = [];

	var yearFaultTable = null;
	var reasonFaultTable = null;

	var yearFaultsMap = new Map();
	var reasonFaultsMap = new Map();

	this.load = function() {
		initLineFaultData();
	};
	this.resizeChart = function() {
		if (yearBarChart) {
			yearBarChart.getChart().resize();
		}
		if (reasonBarChart) {
			reasonBarChart.getChart().resize();
		}
		if (reasonPieChart) {
			reasonPieChart.getChart().resize();
		}
	};
	function initLineFaultData() {
		initFaultTables();
		if (lineId && lineId.length > 0) {
			$.getJSON('/rest/fault/getFaultsByEquipmentId/' + lineId, function(
					result) {
				lineFaultDatas = result;
				drawChart();
			});
		}
	}
	function initFaultTables() {
		var option = MyTable.getDefaultOptions();
		option.paging = true;
		option.pageLength = 10;
		option.searching = true;
		var columns = [ '故障设备', '故障时间', '故障类型', '故障原因', '故障相别', '备注' ];
		yearFaultTable = new MyTable('yearFaultTable', columns, option).show();
		reasonFaultTable = new MyTable('reasonFaultTable', columns, option)
				.show();
	}
	function drawChart() {
		initChartData();
		drawYearBarChart();
		drawReasonBarChart();
		drawReasonPieChart();
	}
	function initChartData() {
		$('#countSpanNumber').html(lineFaultDatas.length);
		lineFaultDatas.sort(sortData);
		for (var i = 0; i < lineFaultDatas.length; i++) {
			var fault = lineFaultDatas[i];

			var faultTime = new Date(
					fault.faultTime.time ? fault.faultTime.time
							: fault.faultTime);
			var year = faultTime.getFullYear();
			var yearData = yearFaultsMap.get(year);
			if (!yearData) {
				yearData = [];
				yearFaultsMap.put(year, yearData);
			}
			yearData.push(fault);

			var faultReson = fault.faultReson;
			var reasonData = reasonFaultsMap.get(faultReson);
			if (!reasonData) {
				reasonData = [];
				reasonFaultsMap.put(faultReson, reasonData);
			}
			reasonData.push(fault);
		}
		function sortData(obj1, obj2) {
			var t1 = obj1.faultTime.time ? obj1.faultTime.time : obj1.faultTime;
			var t2 = obj2.faultTime.time ? obj2.faultTime.time : obj2.faultTime;
			return t1 - t2;
		}
	}
	function drawYearBarChart() {
		if (lineFaultDatas.length == 0) {
			$('#yearBarChart').html('无故障');
			$('#yearBarChart').addClass('no-data');
		} else {
			yearBarChart = new CategoryBar("yearBarChart");
			var x = [];
			var y = [];
			yearFaultsMap.each(function(index, key, value) {
				x.push(key);
				y.push(value.length);
			});
			yearBarChart.setxAxis(x);
			yearBarChart.setyAxis(y).setyAxisName("故障次数");
			yearBarChart.setMagicType(false).setZoomTool(false).setRestore(
					false);
			var opt = yearBarChart.getOption();
			opt.legend.show = false;
			yearBarChart.show();
			yearBarChart.getChart().on('click', function(params) {
				drawYearFaultTable(params.name);
			});
		}
	}
	function drawReasonBarChart() {
		if (lineFaultDatas.length == 0) {
			$('#reasonBarChart').html('无故障');
			$('#reasonBarChart').addClass('no-data');
		} else {
			reasonBarChart = new CategoryBar("reasonBarChart");
			var x = [];
			var y = [];
			// 原因为'其它'的，放到最后
			var otherType = 0;
			reasonFaultsMap.each(function(index, key, value) {
				if (key != '其它') {
					x.push(key);
					y.push(value.length);
				} else {
					otherType += value.length;
				}
			});
			if (otherType > 0) {
				x.push('其它');
				y.push(otherType);
			}
			reasonBarChart.setxAxis(x);
			reasonBarChart.setyAxis(y).setyAxisName("故障次数");
			reasonBarChart.setMagicType(false).setZoomTool(false).setRestore(
					false);
			var opt = reasonBarChart.getOption();
			opt.legend.show = false;
			reasonBarChart.show();
			reasonBarChart.getChart().on('click', function(params) {
				drawReasonFaultTable(params.name);
			});
		}
	}
	function drawReasonPieChart() {
		if (lineFaultDatas.length == 0) {
			$('#reasonPieChart').html('无故障');
			$('#reasonPieChart').addClass('no-data');
		} else {
			reasonPieChart = new Pie("reasonPieChart");
			var data = [];
			reasonFaultsMap.each(function(index, key, value) {
				data.push([ key, value.length ]);
			});
			reasonPieChart.setPie(data);
			reasonPieChart.setLegendPosition('right', 'bottom');
			reasonPieChart.show();
			reasonPieChart.getChart().on('click', function(params) {
				drawReasonFaultTable(params.name);
			});
		}
	}
	function drawYearFaultTable(year) {
		if (year != yearFaultTable.cyear) {
			yearFaultTable.cyear = year;
			var faults = yearFaultsMap.get(year);
			yearFaultTable.reload(getDataset(faults));
			showDiv("yearFaultTableDiv");
		} else {
			yearFaultTable.cyear = 0;
			hideDiv("yearFaultTableDiv");
		}
	}
	function drawReasonFaultTable(reason) {
		if (reason != reasonFaultTable.creason) {
			reasonFaultTable.creason = reason;
			var faults = reasonFaultsMap.get(reason);
			reasonFaultTable.reload(getDataset(faults));
			showDiv("reasonFaultTableDiv");
		} else {
			reasonFaultTable.creason = 0;
			hideDiv("reasonFaultTableDiv");
		}
	}
	function getDataset(faults) {
		var dataset = [];
		for ( var i in faults) {
			var f = faults[i];
			var faultTime = new Date(f.faultTime.time ? f.faultTime.time
					: f.faultTime);
			dataset.push([ getStr(f.equipment), faultTime.toLocaleString(),
					getStr(f.faultType), getStr(f.faultReson),
					getStr(f.faultPhase), getStr(f.remark) ]);
		}
		return dataset;
	}
	function hideDiv(id) {
		$('#' + id).collapse('hide');
	}
	function showDiv(id) {
		$('#' + id).collapse('show');
	}
	function getStr(text) {
		return text ? text : '';
	}
	
};